//退出登录
function exitLogin() {
  if ($(".exit").html() == "注册") {
    location.href = "login_page.html";
  } else {
    //清除本地 localStorage 的用户数据
    if (confirm("是否退出登录")) {
      localStorage.removeItem("username");
      localStorage.removeItem("token");
      localStorage.removeItem("uid");
      showUser();
      // alert("已退出登录");
    }
  }
}
//去登录
function goLogin() {
  if ($(".user").html() == "登录") {
    location.href = "login_page.html";
  } else {
    location.href = "uploadshop.html";
  }
}
//登录成功后，首页上方的显示
function showUser() {
  let username = localStorage.getItem("username");
  if (username) {
    //保留前三位和后两位，其他替换成*
    let newUsername =
      username.substring(0, 3) + "*****" + username.substring(8, 11);
    // console.log(newUsername);
    $(".user").html(newUsername);
    $(".exit").html("退出");
    $("#reg-tips").html("登录成功，开始享受福利");
  } else {
    $(".user").html("登录"); //初始值
    $(".exit").html("注册");
    $("#reg-tips").html("立即注册领取新人福利");
  }
}
showUser();
$(".sousuo").on("click", function () {
  location.href = "sousuo.html";
});
//跳转到首页
function toindex() {
  location.href = "index.html";
}
//跳转到购物车
function toshopcar() {
  if (localStorage.getItem("username")) {
    location.href = "shopcar.html";
  } else {
    alert("请先登录");
  }
}
// 封装一个倒计时功能
function countdown(overtime, a) {
  //overtime传入形式 2021-10-01 12:00:00
  let d2 = new Date(overtime); //d2代表倒计时结束的日期
  //定时器
  let d1 = new Date(); //现在的时间
  let timeDifference = d2 - d1; //获取到毫秒差
  //   console.log(timeDifference);
  let second = parseInt(timeDifference / 1000) % 60; //   获取  零秒
  let minutes = parseInt((timeDifference / 1000 / 60) % 60); //获取 零分
  let hour = parseInt(timeDifference / 1000 / 60 / 60); //获取 小时
  //   var day = parseInt(timeDifference / 1000 / 60 / 60 / 24); //获取 天
  if (second < 10) {
    second = "0" + second;
  }
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (hour < 10) {
    hour = "0" + hour;
  }
  //当到达指定时间后，清除定时器，不然会显示负数
  if (timeDifference <= 0) {
    //当时间差等于零，就清除返回值timer
    clearInterval(timer);
    console.log("本场倒计时结束");
  }
  // let timeArr = [second, minutes, hour];
  //将数据传入页面中，获取a标签
  let aArr = document.querySelectorAll(`${a} a`);
  // console.log(aArr);
  aArr[0].innerHTML = hour;
  aArr[1].innerHTML = minutes;
  aArr[2].innerHTML = second;
  // return timeArr;
}
